﻿using KbtSync.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common.Sql;
using System.Data;

namespace KbtSync.DAL
{
    public class SunlikeDAL
    {
        string connStr;
        public SunlikeDAL(string connStr)
        {
            this.connStr = connStr;
        }
        /// <summary>
        /// 根据产品编码,版本获取库存信息
        /// 注:版本暂不支持!!!!!
        /// </summary>
        /// <param name="erpItemCode"></param>
        /// <param name="version"></param>
        /// <returns></returns>
        public List< ErpStorageInfo> GetStorageInfoes(string erpItemCode,double version=0)
        {
            List<ErpStorageInfo> list = null;
           string sql= "SELECT a.[WH] AS WhCode, b.[NAME] AS WareHouse,[PRD_NO] AS ErpItemCode ,[QTY] AS StockQty " +
                " FROM[DB_KBT].[dbo].[PRDT1]  AS a LEFT JOIN[DB_KBT].[dbo].[MY_WH]  AS b ON a.[WH] = b.[WH]"+
                " WHERE [PRD_NO]='" + erpItemCode + "'";
            DataTable dt = SqlHelper.GetDataTable(connStr, sql);
            if(dt.Rows.Count>0)
            {
                list = new List<ErpStorageInfo>();
                foreach (DataRow r in dt.Rows)
                {
                    list.Add(DataRow2StorageResult(r));
                }                
            }
            return list;
        }
        /// <summary>
        /// 获取根据产品编码,版本和仓库名称获取库存数量
        /// 注:版本暂不支持!!!!!
        /// </summary>
        /// <param name="erpItemCode"></param>
        /// <param name="version"></param>
        /// <param name="warehouse"></param>
        /// <returns></returns>
        public double GetStorageQty(string erpItemCode,string warehouse,double version=0)
        {
            string sql = "SELECT [QTY] AS StockQty " +
                 " FROM [DB_KBT].[dbo].[PRDT1]  AS a LEFT JOIN[DB_KBT].[dbo].[MY_WH]  AS b ON a.[WH] = b.[WH]" +
                 " WHERE [PRD_NO]='" + erpItemCode + "' AND b.[NAME]='" + warehouse + "'";
            var ans = SqlHelper.ExecuteScalar(connStr,CommandType.Text, sql);

            return Convert.ToDouble( ans);
        }

        private ErpStorageInfo DataRow2StorageResult(DataRow row)
        {
            ErpStorageInfo info = new ErpStorageInfo
            {
                WareHouseCode=row["WhCode"].ToString(),
                ErpItemCode = row["ErpItemCode"].ToString(),
                Version=0,//版本信息,暂定统一为0,不支持
                WareHouse = row["WareHouse"].ToString(),
                StockQty = Convert.ToDouble(row["StockQty"])
            };
            
            return info;
        }

        internal bool DbReady()
        {
            string sql = " Select Count(*) FROM [DB_KBT].[dbo].[PRDT1]";
            try
            {
                var ans = SqlHelper.ExecuteScalar(connStr, CommandType.Text, sql);
                return Convert.ToInt64(ans) > 0;
            }
            catch (Exception ex)
            {
                return false;
            }
            
        }
    }
}
